package com.test.demo;


import org.openqa.selenium.*;

public class BossAutoSendMsg {
    private WebDriver driver;
    private WebDriver.Navigation navigation;
    private JavascriptExecutor js;

    private static final String URL = "https://www.zhipin.com/chat/im";
    private static final String REPLY_XPATH = "//*[@id=\"container\"]/div[1]/div[2]/div[3]/div[1]/ul[2]/li[%d]/a/div[2]/div/p";
    private static final String MSG_BOX_XPATH = "//*[@id=\"container\"]/div[1]/div[2]/div[4]/div[2]/div[2]/div[2]/div[2]";
    private static final String HELLO_MSG_1 = "你好，我是方策实业贾小姐，我看了你的简历觉得你挺符合我们的要求，可以沟通下吗？";
    private static final String HELLO_MSG_2 = "你好，目前是在找销售工作吗？";

    public BossAutoSendMsg(WebDriver driver) {
        this.driver = driver;
        this.navigation = driver.navigate();
        this.js = (JavascriptExecutor) driver;
    }

    /**
     * 延时
     * @param second 秒
     */
    private void delay(double second){
        try {
            Thread.sleep((long) (second * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void test() {
        navigation.to(URL);
        delay(20);
        WebElement msgBoxEle = driver.findElement(By.xpath(MSG_BOX_XPATH));
        int sendCount = 0;
        int i = 1;
        while (i < 300 && sendCount < 200) {
            WebElement replyEle = driver.findElement(By.xpath(String.format(REPLY_XPATH, i)));
            String replyTxt = replyEle.getText();
            System.out.println(replyTxt);
            js.executeScript("arguments[0].scrollIntoView();", replyEle);
            if ("您好！在吗？方便沟通一下吗".equals(replyTxt) || "对您的简历很感兴趣，期待深入交流。".equals(replyTxt)) {
                js.executeScript("arguments[0].click();", replyEle);
                delay(0.1);
                msgBoxEle.sendKeys(HELLO_MSG_1);
//                msgBoxEle.sendKeys(HELLO_MSG_2);
                msgBoxEle.sendKeys(Keys.ENTER);
                sendCount++;
                System.out.println("【" + sendCount + "】");
            }
            delay(0.08);
            i++;
        }
    }
}
//*[@id="container"]/div[1]/div[2]/div[3]/div[1]/ul[2]/li[1]/a/div[2]/div/p